package com.cg.controller.announcement;

import java.util.Date;
import java.util.List;
import java.util.Map;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.Announcement;
import com.cg.service.impl.AnnouncementServiceImpl;
import com.cg.utils.Constant;
import com.cg.utils.DateUtils;
import com.cg.utils.JpushUtil;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.annotation.SysLog;
import com.cg.utils.validator.ValidatorUtils;
import com.cg.utils.validator.group.AddGroup;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;

@RestController
@RequestMapping("/sys/announcement")
public class AnnouncementController extends AbstractController{
	
	@Autowired
	AnnouncementServiceImpl announcementServiceImpl;
	
	@RequestMapping("/menu")
	public String test() {

		return "redirect:deformity.jsp";

	}

	/**
	 * 所有公告列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:announcement:list")
	public R list(@RequestParam Map<String, Object> params) {

		// 只有超级管理员，才能查看所有管理员列表
		if (getUserId() != Constant.SUPER_ADMIN) {
			params.put("createUserId", getUserId());
		}

//		Announcement announcement= new Announcement();
//		// 查询列表数据
		Query query = new Query(params);
//		announcement.setType(String.valueOf(params.get("type")));
//		params.put("type", String.valueOf(params.get("type")));
		List<Announcement> userList = announcementServiceImpl.queryList(query);
		int total = announcementServiceImpl.queryTotal(query);

		return R.success(total, "", userList);
	}
	
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:announcement:info")
	public R info(@PathVariable("id") Long id){
		Announcement announcement = announcementServiceImpl.queryObject(id);
		
		
		return R.ok().put("announcement", announcement);
	}
	
	/**
	 * 修改公告
	 */
	@SysLog("修改公告")
	@RequestMapping("/update")
	@RequiresPermissions("sys:announcement:update")
	public R update(@RequestBody Announcement announcement){
		
		announcementServiceImpl.update(announcement);
		
		return R.ok();
	}
	
	/**
	 * 保存公告
	 */
	@SysLog("保存公告")
	@RequestMapping("/save")
	@RequiresPermissions("sys:announcement:save")
	public R save(@RequestBody Announcement announcement){
		ValidatorUtils.validateEntity(announcement, AddGroup.class);
//		announcement.setType("1");
		announcement.setCreateTime(DateUtils.dateToDateString(new Date()));
		announcement.setPublishUser(Integer.parseInt(String.valueOf(getUserId())));
		announcementServiceImpl.save(announcement);
		
		//推送消息
		if("2".equals(announcement.getType())) {
//			JpushUtil.buildPushObject_all_all_alert(announcement.getAnnContent());
//			JpushUtil.buildPushObject_android_ios_alias_alert("13983441921","12123123");
//			JpushUtil.buildPushObject_all_all_alert("0000");
//			JpushUtil.buildPushObject_android_and_ios();
//			JpushUtil.buildPushObject_ios_audienceMore_messageWithExtras();
			if("".equals(announcement.getPushUser())) {
				
				
				JPushClient client = new JPushClient(JpushUtil.MASTER_SECRET,JpushUtil.APP_KEY);
				try {
					client.sendNotificationAll(announcement.getAnnContent());
				} catch (APIConnectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (APIRequestException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else {
				JpushUtil.sendPushToSomeOne(announcement.getPushUser(), announcement.getAnnContent(), announcement.getAnnName());
//				JpushUtil.buildPushObject_all_alias_alert("13983441921","12123123");
				try {
					JpushUtil.push(announcement.getAnnContent(), "",announcement.getPushUser(), "1");
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		return R.ok();
	}
	
	
	/**
	 * 删除公告
	 */
	@SysLog("删除公告")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:announcement:delete")
	public R delete(@RequestBody Long[] ids){
		
		announcementServiceImpl.deleteBatch(ids);
		
		return R.ok();
	}
	

}
